package com.urea.system.service.impl;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.Objects;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.urea.common.core.domain.entity.SysUser;
import com.urea.common.exception.ServiceException;
import com.urea.common.utils.SecurityUtils;
import com.urea.system.constants.BusinessConstants;
import com.urea.system.domain.Terminal;
import com.urea.system.domain.TerminalLog;
import com.urea.system.domain.vo.TerminalVo;
import com.urea.system.service.ITerminalLogService;
import com.urea.system.service.ITerminalService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.urea.system.mapper.TerminalNoticeMapper;
import com.urea.system.domain.TerminalNotice;
import com.urea.system.service.ITerminalNoticeService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 终端预警日志Service业务层处理
 * 
 * @author liuzd
 * @date 2023-08-01
 */
@Service
public class TerminalNoticeServiceImpl  extends ServiceImpl<TerminalNoticeMapper, TerminalNotice> implements ITerminalNoticeService
{
    @Autowired
    private TerminalNoticeMapper terminalNoticeMapper;
    @Autowired
    private ITerminalService terminalService;
    @Autowired
    private ITerminalLogService logService;

    /**
     * 查询终端预警日志
     * 
     * @param id 终端预警日志主键
     * @return 终端预警日志
     */
    @Override
    public TerminalNotice selectTerminalNoticeById(Long id)
    {
        return terminalNoticeMapper.selectTerminalNoticeById(id);
    }

    /**
     * 查询终端预警日志列表
     * 
     * @param terminalNotice 终端预警日志
     * @return 终端预警日志
     */
    @Override
    public List<TerminalVo> selectTerminalNoticeList(TerminalVo terminalNotice)
    {
        SecurityUtils.getUserIdByUserType(terminalNotice);
        return terminalNoticeMapper.selectTerminalNoticeList(terminalNotice);
    }

    /**
     * 新增终端预警日志
     * 
     * @param terminalNotice 终端预警日志
     * @return 结果
     */
    @Override
    public int insertTerminalNotice(TerminalNotice terminalNotice)
    {
        return terminalNoticeMapper.insertTerminalNotice(terminalNotice);
    }

    /**
     * 修改终端预警日志
     * 
     * @param terminalNotice 终端预警日志
     * @return 结果
     */
    @Override
    public int updateTerminalNotice(TerminalNotice terminalNotice)
    {
        return terminalNoticeMapper.updateTerminalNotice(terminalNotice);
    }

    /**
     * 批量删除终端预警日志
     * 
     * @param ids 需要删除的终端预警日志主键
     * @return 结果
     */
    @Override
    public int deleteTerminalNoticeByIds(Long[] ids)
    {
        return terminalNoticeMapper.deleteTerminalNoticeByIds(ids);
    }

    /**
     * 删除终端预警日志信息
     * 
     * @param id 终端预警日志主键
     * @return 结果
     */
    @Override
    public int deleteTerminalNoticeById(Long id)
    {
        return terminalNoticeMapper.deleteTerminalNoticeById(id);
    }

    /**
     * 处理消息
     *
     * @param vo 消息记录
     */
    @Override
    @Transactional
    public void handleMsg(TerminalVo vo) {
        if (Objects.isNull(vo.getAddNum())&&"1".equals(vo.getMsgHandleType())){
            throw new ServiceException("选择定量添加,请填写添加数量!");
        }
        // 处理记录
        TerminalNotice terminalNotice = this.selectTerminalNoticeById(vo.getNoticeId());
        if (Objects.isNull(terminalNotice)){
            throw new ServiceException("参数错误,信息记录为空!");
        }
        TerminalNotice updateNotice = new TerminalNotice();
        updateNotice.setId(vo.getNoticeId());
        // 已处理
        updateNotice.setStatus("1");
        updateNotice.setUpdatedTime(LocalDateTime.now());
        this.updateById(updateNotice);
        // 修改当前容量
        TerminalVo terminal = terminalService.selectTerminalById(vo.getId());
        if (Objects.isNull(terminal)){
            throw new ServiceException("参数错误,设备信息为空!");
        }
        Terminal updateTerminal = new Terminal();
        updateTerminal.setId(vo.getId());
        // 记录日志
        TerminalLog log = new TerminalLog();
        if ("1".equals(vo.getMsgHandleType())){
            if (terminal.getCurrentCapacity().add(vo.getAddNum()).compareTo(terminal.getCapacity())>0){
                throw new ServiceException("更新完设备容量超过设备总容量,请输入正确加注数量");
            }
            updateTerminal.setCurrentCapacity(terminal.getCurrentCapacity().add(vo.getAddNum()));
            log.setChangeCapacity(vo.getAddNum());
        }else{
            updateTerminal.setCurrentCapacity(terminal.getCapacity());
            log.setChangeCapacity(terminal.getCapacity());
        }
        terminalService.updateById(updateTerminal);
        log.setTerminalId(vo.getId());
        log.setTerminalCapacity(terminal.getCurrentCapacity());
        log.setOperater(String.valueOf(SecurityUtils.getUserId()));
        log.setLogType(0);
        logService.save(log);
    }
}
